package org.elip.stewiemaze.server.services;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.logging.Logger;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.elip.stewiemaze.client.DatastoreService;
import org.elip.stewiemaze.server.DatastoreHelper;
import org.elip.stewiemaze.shared.entities.Player;

import com.google.gwt.user.server.rpc.RemoteServiceServlet;

public class DatastoreServiceImpl extends RemoteServiceServlet implements DatastoreService {
	

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	@Override
	public Player retrievePlayer(String nickname, String password) {
		Player player = DatastoreHelper.verifyUsernameAndPassword(nickname, password);
		if (player != null) return player;
		return null;
	}

	@Override
	public String[] retrieveRecordsTable() {
		String[] scores = DatastoreHelper.getScores();
		return scores;
	}

	@Override
	public String[] updateScore(String nickname, String newScore) {
		return DatastoreHelper.updateScore(nickname, Long.parseLong(newScore));
	}
	
	@Override
	public Player retrieveFacebookPlayer(String nickname) {
		
		Logger logger = Logger.getLogger(DatastoreServiceImpl.class.getName());
		logger.fine("Retrieving Player with nickname " + nickname + " from database");
		Player player = DatastoreHelper.getPlayer(nickname);
		logger.fine("got player " + player + " from database");
		if (player != null) return player;
		return null;
	}

}
